﻿using M_Process.API.Appliction.Command.ProductionProcessCommand;
using M_Process.Domain;
using M_Process.Domain.DTO;
using M_Process.Repository.ProcessInterface;
using MediatR;

namespace M_Process.API.Appliction.CommandHandlers.ProductionProcessCommandHandler
{
    /// <summary>
    /// 显示工序信息命令处理器
    /// </summary>
    public class ShowProductionProcessCommandHandler : IRequestHandler<ShowProductionProcessCommand, ProductionProcessDto<ProductionProcess>>
    {
        /// <summary>
        /// 工序仓储接口
        /// </summary>
        protected readonly IProductionProcessRepository productionProcessRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionProcessRepository">工序仓储接口</param>
        public ShowProductionProcessCommandHandler(IProductionProcessRepository productionProcessRepository)
        {
            this.productionProcessRepository = productionProcessRepository;
        }

        /// <summary>
        /// 处理显示工序信息命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ProductionProcessDto<ProductionProcess>> Handle(ShowProductionProcessCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await productionProcessRepository.GetAllAsync()
                       where !a.IsDeleted
                       select new ProductionProcess()
                       {
                           ProductionProcessesId = a.ProductionProcessesId,
                           ProductionProcessesName = a.ProductionProcessesName,
                           ProductionProcessesNumber = a.ProductionProcessesNumber,
                           Specification = a.Specification,
                           Status = a.Status,
                           Notes = a.Notes,
                           ProcessClassification=a.ProcessClassification,
                           CreatedDate = a.CreatedDate,
                       };

            if (!string.IsNullOrEmpty(request.ProductionProcessesName))
            {
                list = list.Where(x => x.ProductionProcessesName.Contains(request.ProductionProcessesName));
            }

            if (!string.IsNullOrEmpty(request.ProductionProcessesNumber))
            {
                list = list.Where(x => x.ProductionProcessesNumber.Contains(request.ProductionProcessesNumber));
            }

            if (!string.IsNullOrEmpty(request.Status))
            {
                list = list.Where(x => x.Status==request.Status);
            }

            if (request.PageIndex == null)
            {
                request.PageIndex = 1;
            }

            if (request.PageSize == null)
            {
                request.PageSize = 15;
            }

            ProductionProcessDto<ProductionProcess> page = new ProductionProcessDto<ProductionProcess>();
            page.TotalCount=list.Count();
            page.PageCount= (int)Math.Ceiling(page.TotalCount * 1.0 / (int)request.PageSize);
            page.list=list.OrderByDescending(x=>x.CreatedDate).Skip((int)request.PageIndex-1).Take((int)request.PageSize).ToList();
            return page;
        }
    }
}
